package util;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Properties;

public class EmailUtil {
    public static final String SMTPSERVER = "smtp.126.com";
    public static final String SMTPPORT = "465";
    public static final String ACCOUT = "tangjixiong@126.com";
    public static final String PWD = "RBOOPEWDJPBSWSWM";
    public static Session session=null;
    //创建邮件会话
    public static Session getSession(){
        // 创建邮件配置
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.port", SMTPPORT);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
        props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl

        try {
            // 根据邮件配置创建会话，注意session别导错包
            Session session = Session.getDefaultInstance(props);
            // 开启debug模式，可以看到更多详细的输入日志
            session.setDebug(true);
           return session;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static MimeMessage createEmail(Session session,String toMail,String subject,String text) throws Exception {
        // 根据会话创建邮件
        MimeMessage msg = new MimeMessage(session);
        // address邮件地址, personal邮件昵称, charset编码方式
        InternetAddress fromAddress = new InternetAddress(ACCOUT,"蟹堡王", "utf-8");
        // 设置发送邮件方
        msg.setFrom(fromAddress);
        InternetAddress receiveAddress = new InternetAddress(
                toMail, "验证码", "utf-8");
        // 设置邮件接收方
        msg.setRecipient(Message.RecipientType.TO, receiveAddress);
        // 设置邮件标题
        msg.setSubject(subject, "utf-8");
        msg.setText(text);
        // 设置显示的发件时间
        msg.setSentDate(new Date());
        // 保存设置
        msg.saveChanges();
        return msg;
    }

    public static void sendMail(String toMail,String subject,String text){
        try{
            if(session==null){
                //创建会话
                session=getSession();
            }
            //创建邮件
            MimeMessage message = createEmail(session,toMail,subject,text);
            //获取传输通道
            Transport transport = session.getTransport();
            transport.connect(SMTPSERVER,ACCOUT, PWD);
            //连接，并发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //发送验证码邮件
    public static void sendCode(String to, HttpSession session){
        String subject="蟹堡王注册验证码";
        Integer code=(int)(Math.random()*(9999-1000)+1000);
        String text="您的验证码为："+code+"，请勿泄露于他人！";
        session.setAttribute("code",code);
        sendMail(to,subject,text);
    }


}